set more off
capture log close
capture clear all
*log using National_banks_crisis6.log, text replace
set memory 200m

*
***!***!***!***!***!***!***!***!***!***!***!***!***!***!***!***!***/


/***Define Global Directory ****/
//Change rootdir to where store files
	global ROOTDIR "F:/Dropbox/national banks peg/Replication_files"
	local PROGDIR  "$ROOTDIR"
	local INDIR  "$ROOTDIR/data"
	local OUTDIR  "$ROOTDIR/output"
/*******************************/


cd "`INDIR'"

use  National_bank_call_reports_states, clear
/** Merge location labels*/
merge m:1 location using state_abbrev.dta
drop _merge

tsset  loc_no call_date_order


gen fifth = mod(call_date_order,5)



//Prepare series for factor analysis
replace total_assets = total_assets - bonds_circulation_assets

gen total_equity = capital_stock_liab+ surplus_fund_liab+ undivided_profits_liab+ dividends_unpaid_liab

gen lev = (total_assets-total_equity)/total_assets

foreach var of varlist lev*{
gen d`var' = `var' - l.`var'
gen dln`var'= ln(`var')-ln(l.`var')
}



keep dlev* call_date loc* fifth call_date_order


gen year = year(call_date)
gen month = month(call_date)



drop loc_no
egen loc_no = group(location)

drop location

reshape wide dlev, i(call_date) j(loc_no)

drop if _n==1

foreach var of varlist dlev*{






forval i = 0/4{

egen temps = mean(`var') if fifth ==`i'
replace `var' = `var'-temps if fifth==`i'
drop temps

}

egen m = mean(`var')
replace `var'= `var'-m
drop m


count if `var' == . & year(call_date)>=1880



if r(N)>0{
drop `var'
}





}
